<template>
	<el-container>
		<el-aside class="nopadding treesearch" style="width: 200px;background-color: #fff;" >
			<ex-search-tree ref="searchTree" :treeApi="this.$API.system.dic.treeData" @changeHandler="searchTreeHandler" :param="{dictType:'0'}" :title="title">
			</ex-search-tree>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
				<fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
				</div>
				<div class="right-panel">
					<ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="dictName" placeholder="请输入字典名称" :initParams="{dictType:'1'}"></ex-search>
				</div>
			</el-header>
			<el-main class="nopadding">
				<ex-table id="dicTable" ref="table" :columns="columns" :url="this.$API.system.dic.list.url"
				:refs="this.$refs" search="search" @columnClick="columnClickHandler"
				@checkClick="checkClickHandler"></ex-table>
			</el-main>
		</el-container>
	</el-container>



	<save-dialog  ref="saveDialog" @success="searchHandler"></save-dialog>
	<view-dialog ref="viewDialog"/>

  </template>

  <script>
 import saveDialog from './save.vue'
 import viewDialog from './view'
  import ExTable from "@/components/exTable/index.vue";
  import ExSearch from "@/components/exSearch/index.vue";
  import ExTreeNavigation from "@/components/exSearchTree/index.vue";
  import ExSearchTree from "@/components/exSearchTree/index.vue";
 import BaseTemplate from "@/utils/baseTemplate";
  //import dict from "@/utils/dict";

  export default {
	name: '数据字典',
	components: {
	  ExSearchTree,
	  ExTreeNavigation,
	  ExSearch,
	  ExTable,
	  saveDialog,
	  viewDialog
	},
	data() {
	  return {
		dialog: {
		  save: false,
		  permission: false
		},
		apiObj: [],
		buttons: [
        {name: this.$t('button.add'), icon: "el-icon-plus", click: "add", width:"70px",permission:"add"},
        {name: this.$t('button.edit'), icon: "el-icon-edit", type: "parent",parent:true,show:"none",width:"70px",
        childrenbuttons:[
          //{name: this.$t('button.edit'),  click: "edit",permission:"edit"},
          {name: this.$t('button.delete'),  click: "del",permission:"delete"},
          {name: this.$t('button.enabled'),  click: "enabled",permission:"enabled"},
          {name: this.$t('button.disabled'),  click: "disabled",permission:"disabled"},
        ]},
        {name: "...", type: "parent",parent:true,permission:"export",
        childrenbuttons:[
          {name: this.$t('button.export'),  click: "export",permission:"export"},
        ]},
      ],
		searches: [
		  {type: "input", name: "字典名称", code: "dictName", placeholder: "请输入字典名称", maxlength: 30, showLimit: true},
		  {type: "input", name: "字典编码", code: "dictCode", placeholder: "请输入字典编码", maxlength: 30, showLimit: true},
		  {type: "radio", name: "类型", code: "dictType",dictCode:'DICT_TYPE'},
		],
		columns: [
    {code: "操作", name: "操作", width: "100",
      click:[{label:"",icon:"el-icon-edit-pen",event:'detailedit',permission: "edit"},
      {label:"",icon:"el-icon-delete",event:'detaildel',permission: "delete"}]},
      {code: "dictName", name: "名称", width:"",click: "show1",formatter: function (row,value,_this){
			  if(row.dictValue){
				  return _this.$cache.dic.exLabel(row.dictCode,row.dictValue,true);
			  }
			 return value;
			}},
      {code: "dictValue", name: "字典值", width:"120"},
      {code: "parentLevelCode", name: "字典组", width:"200",formatter:"DICT",key:"SysDictionary"},
      {code: "dictCode", name: "编码", width:"250"},
		  {code: "dictType", name: "类型", width:"100",formatter:"DICT",key:"DICT_TYPE"},
		  {code: "isEnabled", name: "是否可用", width:"100",sortable:true,formatter: "DICT",key: "IS_ENABLED"},
		  // {code: "createUser", name: "创建人", width:"100",formatter: "DICT",key: "SysUser"},
		  // {code: "createTime", name: "创建时间", width:"150",sortable:true,formatter: "DATETIME"},
		],
		parentLevelCode:"",
		title:"数据字典"
	  }
	},
	created() {
	},
	mounted() {
	},
	methods: {
		checkClickHandler(value) {
      if(value==0){
        for (let index = 0; index < this.buttons.length; index++) {
          if(this.buttons[index].show!=undefined){
            this.buttons[index].show="none"
          }
        }
      }else{
        for (let index = 0; index < this.buttons.length; index++) {
          if(this.buttons[index].show!=undefined){
          this.buttons[index].show=""
          }
        }
      }
    },
	  buttonHandler(index, name) {
		this.$options.methods[name](this, index);
	  },
	  async searchTreeHandler(data) {
		let queryParams = {pathCode:data.levelCode}
		this.parentLevelCode=data.levelCode;
		await this.$refs.table.refresh(queryParams);
	  },
	  async searchHandler() {
		  let queryParams = {pathCode: this.parentLevelCode}
		  await this.$refs.table.refresh(queryParams);
	  },
	  columnClickHandler(name,data){
		this.$options.methods[name](this, data);
	  },
	  //添加
	  add(_this) {
		let data = {
			parentLevelCode: _this.parentLevelCode,
			entityName:_this.$CONSTANT.entity.SysDictionary.name,
		};
		_this.$nextTick(() => {
			_this.$refs.saveDialog.open(data);
		})
	  },
	  //编辑
	  edit(_this) {
		let row = _this.$refs.table.selectionOne();
		if (!row) {
			return;
		}
		_this.$nextTick(() => {
			_this.$refs.saveDialog.open(row);
		})
	  },
     //单个编辑
     detailedit(_this,row) {
      _this.$nextTick(() => {
        _this.$refs.saveDialog.open(row);
      })
    },
	  //查看
	  show(_this) {
      let row = _this.$refs.table.selectionOne();
      if (!row) {
        return;
      }
      _this.$nextTick(() => {
        _this.$refs.viewDialog.open(row);
      })
    },
	//查看
	show1(_this,row) {
		_this.$nextTick(() => {
			_this.$refs.viewDialog.open(row);
		})
	},
    //导出
    async export(_this,index) {
      let attachParams = {};
      let exportApi = _this.$API.system.dic.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
    },
	  //批量删除
	  del(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定删除选中的 ${selections.length} 项吗？如果删除项中含有子集将会被一并删除`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.dic.deleteByIds.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
    //单个删除
   detaildel(_this, row) {
      let selections = [];
      selections.push(row);
      _this.$confirm(`确定删除选中的 ${selections.length} 项吗？如果删除项中含有子集将会被一并删除`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.dic.deleteByIds.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
    //批量启用
    enabled(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定启用选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.dic.enabled.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
    //批量禁用
    disabled(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定禁用选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.dic.disabled.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
	  },
	}
  }
  </script>
